/**
 * 表结构删除数据库验证界面
 * @author miaoyl
 * @date 2018/01/03 16:00
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd:false,
            isEdit:false,
            isDel:false,
            form: {
                USERNAME: null,
                PASSWORD: ''
            },
            rules: {
                USERNAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                PASSWORD: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            }
        },
        callObj,
        pageVue,
        delData,
        cb
    ;

    var DelTest = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _tag: 'table_del_test',
                _loadTplsFull: [{
                    dom: 'table_edit', tpl: 'system/table_edit'
                }]
            }, win);
        },

        editParam: function () {
            app.ajax({
                xurl: 'sys/param/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '全局参数信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },

        addParam: function () {
            app.ajax({
                xurl: 'sys/param/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '全局参数信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('param').reloadTableCurr();
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            if(obj.is_add){
                vueData.isAdd = true;
            }else {
                vueData.isAdd = false;
            }
            if(obj.is_edit){
                vueData.isEdit = true;
            }else {
                vueData.isEdit = false;
            }
            if(obj.is_del){
                vueData.isDel = true;
            }else {
                vueData.isDel = false;
            }
            app.extendUpLow({
                target: vueData.form,
                res: {},
                reset: true
            });
            delData = obj.obj;
            cb = obj.cb;
        },
        _ready: function () {
            pageVue = new Vue({
                el: '#table_del_test',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                app.ajax({
                                    xurl: 'sys/table/delTest',
                                    xdata: {USERNAME: vueData.form.USERNAME, PASSWORD: vueData.form.PASSWORD},
                                    loading: true,
                                    confirm: {msg: "是否确认?"},
                                    //okmsg: '表结构信息删除成功',
                                    cb: function (result) {
                                        if (result.ok) {
                                            //app.pageModule('columns').reloadTableCurr();
                                            cb(true);
                                            self._closeTpl();
                                            if(vueData.isAdd){
                                                self._openTplFull({
                                                    tag: 'table_edit',
                                                    moduleName: self._moduleName,
                                                    tagData: {
                                                        is_add: true
                                                    },
                                                    index: 0,
                                                    openOpts:{
                                                        title: self._moduleName + '新增',
                                                        maxmin: true
                                                    }
                                                });
                                            }
                                            if(vueData.isEdit){
                                                self._openTplFull({
                                                    tag: 'table_edit',
                                                    moduleName: self._moduleName,
                                                    tagData: {
                                                        obj:delData
                                                    },
                                                    index: 0,
                                                    openOpts:{
                                                        title: self._moduleName + '信息编辑',
                                                        maxmin: true
                                                    }
                                                });
                                            }
                                            if(vueData.isDel){
                                                app.ajax({
                                                    xurl: 'sys/table/deleteDataBatch',
                                                    xdata: delData,
                                                    loading: true,
                                                    toJsonStr: true,
                                                    confirm: {msg: "是否确认删除?"},
                                                    okmsg: self._moduleName + '信息删除成功',
                                                    cb: function (result) {
                                                        if (result.ok) {
                                                            app.pageModule('columns').reloadTableCurr();
                                                        }
                                                    }
                                                });
                                            }
                                           // self.reloadTableCurr();
                                        }
                                    }
                                });
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('table_del_test');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new DelTest();

});